package com.qeeyou.apps.android.craftsman.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.HelpItemInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2017/12/17.
 */

public class PopWindowListAdapter extends BaseAdapter {

    private Activity mActivity;
    private List<String> itemList;
    private List<Boolean> selectList;
    private LayoutInflater mLayoutInflater;

    public PopWindowListAdapter(Activity activity, List<String> itemList) {
        mActivity = activity;
        mLayoutInflater = LayoutInflater.from(mActivity);
        if(this.itemList == null) {
            this.itemList = new ArrayList<String>();
        }
        this.itemList.clear();
        if(itemList != null && itemList.size() > 0) {
            this.itemList.addAll(itemList);
        }
    }

    public void setItems(List<String> itemList) {
        if(this.itemList == null) {
            this.itemList = new ArrayList<String>();
        }
        this.itemList.clear();
        if(itemList != null && itemList.size() > 0) {
            this.itemList.addAll(itemList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        int count = this.itemList != null ? this.itemList.size() : 0;
        return count;
    }

    @Override
    public String getItem(int i) {
        if(i < 0 || i >= this.itemList.size()) {
            return null;
        }
        return this.itemList.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        if(view == null) {
            view = mLayoutInflater.inflate(R.layout.view_item_pop_window_list, null, false);
            holder = new ViewHolder();
            holder.imgvIndicator = (ImageView) view.findViewById(R.id.imgvIndicator4ItemPopList);
            holder.txtvTitle = (TextView) view.findViewById(R.id.txtvName4ItemPopList);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        String itemStr = this.itemList.get(position);
        if(itemStr != null) {
            holder.txtvTitle.setText(itemStr);
            holder.imgvIndicator.setVisibility(View.INVISIBLE);
        }
        return view;
    }

    static class ViewHolder {
        ImageView imgvIndicator;
        TextView txtvTitle;
    }
}
